package vns.server;

import java.util.ArrayList;
import java.util.List;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import vns.client.service.ObjectValueService;
import vns.server.domain.ObjectValue;
import vns.shared.ObjectDTO;
import vns.shared.ObjectPropertyDTO;
import vns.shared.ObjectValueDTO;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("serial")
public class ObjectValueServiceImpl extends RemoteServiceServlet implements
		ObjectValueService {

	@Override
	public ObjectValueDTO Create(ObjectValueDTO ovalueDTO) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		ObjectValue action = null;
		try {
			action = new ObjectValue(ovalueDTO);
			pm.makePersistent(action);
		} finally {
			pm.close();
		}
		return action.toDTO();
	}

	@Override
	public boolean Remove(Key id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			ObjectValue action = pm.getObjectById(ObjectValue.class, id);
			if (action != null) {
				pm.deletePersistent(pm.getObjectById(ObjectValue.class, id));
			}
		} finally {
			pm.close();
		}
		return true;
	}

	@Override
	public ObjectValueDTO Loadbyid(Key id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		ObjectValueDTO actionDTO = null;
		try {
			ObjectValue action = pm.getObjectById(ObjectValue.class, id);
			actionDTO = action.toDTO();
		} finally {
			pm.close();
		}
		return actionDTO;
	}

	@Override
	public ObjectValueDTO Update(ObjectValueDTO ovalueDTO) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		ObjectValue action = null;
		try {
			action = pm.getObjectById(ObjectValue.class, ovalueDTO.getId());
			action.updatefromDTO(ovalueDTO);
		} finally {
			pm.close();
		}

		return action.toDTO();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ObjectValueDTO> Loadall(ObjectDTO objectDTO) {
		List<ObjectValueDTO> listactionDTO = new ArrayList<ObjectValueDTO>();
		List<ObjectValue> listaction = new ArrayList<ObjectValue>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query q = pm.newQuery(ObjectValue.class);
		q.setFilter("objectKey==key");
		q.declareParameters("String key");
		try {
			listaction = (List<ObjectValue>) q.execute(KeyFactory
					.keyToString(objectDTO.getId()));
			for (ObjectValue action : listaction) {
				listactionDTO.add(pm.getObjectById(ObjectValue.class, action)
						.toDTO());
			}
		} finally {
			pm.close();
		}
		return listactionDTO;

	}

	@Override
	public ArrayList<ObjectValueDTO> Loadall(ObjectPropertyDTO opropertyDTO) {
		ArrayList<ObjectValueDTO> listactionDTO = new ArrayList<ObjectValueDTO>();
		// PersistenceManager pm = PMF.get().getPersistenceManager();
		// try {
		// ObjectProperty actionset = pm.getObjectById(ObjectProperty.class,
		// opropertyDTO.getId());
		// Set<Key> actions = actionset.getObjectValue();
		// for (Key action : actions) {
		// listactionDTO.add(pm.getObjectById(ObjectValue.class, action)
		// .toDTO());
		// }
		// } finally {
		// pm.close();
		// }
		return listactionDTO;
	}

	@Override
	public void Updateall(List<ObjectValueDTO> listNew, ObjectDTO objectDTO) {
		// TODO Auto-generated method stub
	}

	@Override
	public void Updateall(List<ObjectValueDTO> listNew,
			ObjectPropertyDTO opropertyDTO) {
		// TODO Auto-generated method stub
	}

	@Override
	public ArrayList<ObjectValueDTO> LoadAll(ObjectDTO objectDTO,
			List<ObjectPropertyDTO> listObjectPropertyDTO) {
		ArrayList<ObjectValueDTO> listactionDTO = new ArrayList<ObjectValueDTO>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		String tempOKey = KeyFactory.keyToString(objectDTO.getId());

		Query q = pm.newQuery(ObjectValue.class);
		q.setFilter("objectKey == okparam");
		q.setFilter("propertyKey == pkparam");
		q.setOrdering("weight asc");
		q.declareParameters("String okparam");
		q.declareParameters("String pkparam");

		try {
			for (ObjectPropertyDTO op : listObjectPropertyDTO) {
				String tempPKey = KeyFactory.keyToString(op.getId());
				if (tempOKey != null && tempOKey != "" && tempPKey != null
						&& tempPKey != "") {
					@SuppressWarnings("unchecked")
					List<ObjectValue> actions = (List<ObjectValue>) q.execute(
							tempOKey, tempPKey);
					// .execute();
					for (ObjectValue action : actions) {
						ObjectValue term = pm.detachCopy(action);
						listactionDTO.add(term.toDTO());
					}
				}
			}
		} finally {
			q.closeAll();
			pm.close();
		}
		return listactionDTO;
	}

	@Override
	public List<ObjectValueDTO> LoadBy(ObjectDTO objectDTO,
			ObjectPropertyDTO objectPropertyDTO) {
		ArrayList<ObjectValueDTO> listactionDTO = new ArrayList<ObjectValueDTO>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		String tempOKey = KeyFactory.keyToString(objectDTO.getId());
		String tempPKey = KeyFactory.keyToString(objectPropertyDTO.getId());

		Query q = pm.newQuery(ObjectValue.class);
		q.setFilter("objectKey == okparam");
		q.setFilter("propertyKey == pkparam");
		q.setOrdering("weight asc");
		q.declareParameters("String okparam");
		q.declareParameters("String pkparam");
		try {
			if (tempOKey != null && tempOKey != "" && tempPKey != null
					&& tempPKey != "") {
				@SuppressWarnings("unchecked")
				List<ObjectValue> actions = (List<ObjectValue>) q.execute(
						tempOKey, tempPKey);
				// .execute();
				for (ObjectValue action : actions) {
					ObjectValue term = pm.detachCopy(action);
					listactionDTO.add(term.toDTO());
				}
			}
		} finally {
			q.closeAll();
			pm.close();
		}
		return listactionDTO;
	}

	@Override
	public void UpdateAll(List<ObjectValueDTO> listValue) {
		List<ObjectValueDTO> listOld = this.LoadAll();
		for (ObjectValueDTO element : listValue) {
			if (!containInList(listOld, element)) {
				element = this.Create(element);
			} else {
				this.Update(element);
			}
		}
		for (ObjectValueDTO element : listOld) {
			if (!containInList(listValue, element)) {
				this.Remove(element.getId());
			}
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ObjectValueDTO> LoadAll() {
		List<ObjectValueDTO> listDTO = new ArrayList<ObjectValueDTO>();
		List<ObjectValue> list = new ArrayList<ObjectValue>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query q = pm.newQuery(ObjectValue.class);
		try {
			list = (List<ObjectValue>) q.execute();
			for (ObjectValue temp : list) {
				listDTO.add(temp.toDTO());
			}

		} finally {
			pm.close();
		}
		return listDTO;
	}

	private boolean containInList(List<ObjectValueDTO> list,
			ObjectValueDTO element) {
		if (element.getId() == null)
			return false;
		for (ObjectValueDTO optDTO : list) {
			if (optDTO.getId().equals(element.getId()))
				return true;
		}
		return false;
	}

}
// viet cho tui cai ham o day, lam viec nhu bên duoi
// UpdateAll(List<ObjectValue> listValue){ // chi con 1 doi so//cho nay la
// ObjectValueDTO chu?
// khoi xet gi het luon
// minh da bo 13 kieu kia roi nhap lon
// ong chi viec luu vao bang value binh thuong hoi

// List<ObjectValue> LoadAll(ObjectDTO)
// @Return load tat ca cac object value của ObjectDTO ra roi return
// e
